/*
 * Event modules
 * author : DUCDQ
 */

 $(function(){
 	//initialized calendar
	$("#startDate").datepicker({showOn: 'button', buttonImage: '/public/library/jquery/calendar/calendar.gif', buttonImageOnly: true,numberOfMonths: 2,showButtonPanel: true, calendarType: 'solar'});
	$("#startDate").change(function()
		{
			if ($("#finishDate").val().length == 0)
				  $("#finishDate").val($(this).val()); //set finishDate to be the same as startDate, automatically
		}
	);
	
	$("#finishDate").datepicker({showOn: 'button', buttonImage: '/public/library/jquery/calendar/calendar.gif', buttonImageOnly: true,numberOfMonths: 2,showButtonPanel: true, calendarType: 'solar'});
   	$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['']));
	$("#startDate").datepicker($.datepicker.regional['vi']);
    $("#finishDate").datepicker($.datepicker.regional['vi']);
	if($("#lang").val() != null){
		//alert($("#lang").val());
		/*
		$("#startDate").datepicker('option', $.extend({showMonthAfterYear: false},
			$.datepicker.regional[$("#lang").val()]));
	    $("#finishDate").datepicker('option', $.extend({showMonthAfterYear: false},
	    	$.datepicker.regional[$("#lang").val()]));
	    */
	 }   	
	$("#calendarType").change(function() {
		$("#startDate").datepicker('option', $.extend({calendarType: $(this).val()}));
        $("#finishDate").datepicker('option', $.extend({calendarType: $(this).val()}));
	});//complete initialized calendar
	
	$("#newEvent").submit(function(){
		if($("#frequency").val() != 'weekly' && $("#frequency").val() != 'daily') 
		{
			var f = $("#startDate").val();
			var t = $("#finishDate").val();
			
			if (f.length < 0)//<=0
			{
				alert("You must chose startDate ");
				return false;
			}
			else
			{
				var a = f.split(/[/.-]/);
				var startDate = parseInt(a[0]) + parseInt(a[1]) * 35 + parseInt(a[2]) * 500;
				if (t.length < 0)//==
				{
					alert("You chose startDate but did not choose finishDate");
					return false;
				}
				else 
				{
					var b = t.split(/[/.-]/);
					var finishDate = parseInt(b[0]) + parseInt(b[1]) * 35 + parseInt(b[2]) * 500;
					if (parseInt(startDate) > parseInt(finishDate))
					{
						alert(startDate + " " + finishDate);
						alert("startDate must be before finishDate");
						return false;
					}
					else if(startDate == finishDate)
					{
						if(parseInt($("#startHour").val()) > parseInt($("#finishHour").val()))
						{
							alert($("#startHour").val() + ' ' + $("#finishHour").val());
							alert("startHour must be before finishHour");
							return false;
						}
						else if (parseInt($("#startHour").val()) == parseInt($("#finishHour").val()) &&
								parseInt($("#startMinute").val()) >= parseInt($("#finishMinute").val()))
						{
							alert("startMinute must be before finishMinute");
							return false;
						}
					}
				}
			}
		}
		else
		{
			var f = 60 * parseInt($("#finishHour").val()) + parseInt($("#finishMinute").val());
			var s = 60 * parseInt($("#startHour").val()) + parseInt($("#startMinute").val());
			//alert(s + '   ' +f);
			if(($("#frequency").val() == 'weekly'  && $("#startDay").val() == $("#finishDay").val()) || $("#frequency").val() == 'daily')
			{  
				//if (parseInt(s) > parseInt(f))
				if (s > f)
				{
					alert("startTime must be before finishTime");
					return false;
				}
			}
		}
     });
	
 	$("#frequency").change(function(e){
 		switch($("#frequency").val())
 		{
 			case 'once' : 
 			case 'monthly': 
 			case 'yearly': 
 			{
 				$("#eventDate").show();
 				$("#eventDay").hide();
 				break;
 			}
 			case 'daily': 
 			{
 				$("#eventDate").hide();
 				$("#eventDay").hide();
	 			break;
 			}
 			case 'weekly': 
 			{
 				$("#eventDate").hide();
 				$("#eventDay").show();
	 			break;
 			}
 		}
 	});
 	
 	//effect list.phtml
 	$("input:checkbox[value='checkall']").click (function(){
    	if ($(this).attr("checked") )
    		//check all the things in name=category[]
    		$("input:checkbox[name='category\[\]']").attr("checked","checked");
    });
    
    $("input:checkbox[value='uncheckall']").click (function(){
	if ($(this).attr("checked") )
	{
		//check all the things in name=category[]
		$("input:checkbox[name='category\[\]']").removeAttr("checked");
		$("input:checkbox[value='checkall']").removeAttr("checked");
		$(this).removeAttr("checked");
	}
    });
 	
 });